package com.thunbu.thunbu_meeting_system.widget

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator

import com.google.android.material.tabs.TabLayout

/**
 * 自定义tab 适配 ui效果
 *
 * 监听实现OnTabSelectedListenerImp 可带动画
 * 不需要 可以自己实现
 */
class ProdeTablayout @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : TabLayout(context, attrs, defStyleAttr) {


    abstract class OnTabSelectedListenerImp : OnTabSelectedListener {

        var oldPos = -1

        val viewArray = mutableMapOf<Int, View?>()


        fun getRefTvView(tab: Tab): View? {
            try {
                val textView = tab.view.javaClass.getDeclaredField("textView")
                textView.isAccessible = true
                return textView[tab.view] as View
            } catch (e: NoSuchFieldException) {
                e.printStackTrace()
            } catch (e: IllegalAccessException) {
                e.printStackTrace()
            }
            return null
        }


        override fun onTabUnselected(tab: Tab?) {
            Log.i("fancl", "onTabUnselected" + tab!!.position + "")
            oldPos = tab.position
            val childView = viewArray[tab.position]
            childView?.let {
                showScaleSmallAnim(tab, childView)
            }

        }

        override fun onTabSelected(tab: Tab?) {
            if (oldPos != tab!!.position) {
                val childView: View? = if (viewArray.contains(tab.position)) {
                    viewArray.get(tab.position)
                } else {
                    val view = getRefTvView(tab)
                    viewArray[tab.position] = view
                    view
                }
                showScaleBigAnim(tab, childView)

            }

        }

        override fun onTabReselected(tab: Tab?) {

        }

        /**
         * 放大动画开始执行
         */
        fun showScaleBigAnim(tab: Tab, view: View?) {
//            view?.let {
//                val currentSet = createBigAnim(it)
//                val old_animatorSet = tab.tag
//                if (old_animatorSet != null && old_animatorSet is AnimatorSet && old_animatorSet.isRunning) {
//                    old_animatorSet.addListener(onEnd = {
//                        tab.setTag(currentSet)
//                        currentSet.start()
//                    })
//                } else {
//                    tab.setTag(currentSet)
//                    currentSet.start()
//                }
//
//            }


        }

        /**
         * 缩小动画开始执行
         */
        fun showScaleSmallAnim(tab: Tab, view: View?) {
//            view?.let {
//                val currentSet = createSmallAnim(it)
//                val old_animatorSet = tab.tag
//                if (old_animatorSet != null && old_animatorSet is AnimatorSet && old_animatorSet.isRunning) {
//                    old_animatorSet.addListener(onEnd = {
//                        tab.tag = currentSet
//                        currentSet.start()
//                    })
//                } else {
//                    tab.tag = currentSet
//                    currentSet.start()
//                }
//
//            }


        }

        /**
         * 创建放大动画
         */
        fun createBigAnim(view: View): AnimatorSet {
            val new_animatorSet = AnimatorSet()
            val animX = ObjectAnimator.ofFloat(view, "scaleX", 1f, 1.05f)
            val animY = ObjectAnimator.ofFloat(view, "scaleY", 1f, 1.05f)

            return new_animatorSet.apply {
                setDuration(50)
                setInterpolator(AccelerateDecelerateInterpolator())
                playTogether(animX, animY)
            }


        }

        /**
         * 创建缩小动画
         */
        fun createSmallAnim(view: View): AnimatorSet {
            val new_animatorSet = AnimatorSet()
            val animX = ObjectAnimator.ofFloat(view, "scaleX", 1.05f, 1.0f)
            val animY = ObjectAnimator.ofFloat(view, "scaleY", 1.05f, 1.0f)
            return new_animatorSet.apply {
                setDuration(50)
                setInterpolator(AccelerateDecelerateInterpolator())
                playTogether(animX, animY)
            }


        }

    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        clearOnTabSelectedListeners() //清除所有监听
    }


}